/*策略模式：为了解决多重if-else的问题*/

//做法：不同的算法提供不同的实现，提供一个统一接口来调用不同的算法，得到不同的实现效果

//场景：游戏制作中，不同的角色使用不同的扣血机制
#include<iostream>

class Character;

class Context//上下文接口
{
    public:
        Context(Character *c):character_(c){};
        Character* getCharacter(){return character_;};
        void setCharacter(Character *c){character_ = c;};
    private:
        Character *character_;
};


class Character  //角色基类
{
    public:
        virtual ~Character(){};
        //扣血机制
        virtual void Hart() = 0;

        //查看血量
        virtual float getBlood() = 0;

        //设置血量
        virtual void setBlood(float blood) = 0;
};

class CharacterA:public Character//继承于Character
{
    public:
        CharacterA(float blood):blood_(blood){};

        virtual void Hart(){std::cout<<"CharacterA扣血算法"<<std::endl;};
        virtual float getBlood(){return blood_;};
        virtual void setBlood(float blood){blood_ = blood;};
    private:
        float blood_;
};

class CharacterB:public Character//继承于Character
{
    public:
        CharacterB(float blood):blood_(blood){};

        virtual void Hart(){std::cout<<"CharacterB扣血算法"<<std::endl;};
        virtual float getBlood(){return blood_;};
        virtual void setBlood(float blood){blood_ = blood;};
    private:
        float blood_;
};

int main(int argc, char const *argv[])
{
    Context x(new CharacterA(100));
    x.getCharacter()->Hart();

    std::cout << "blood" <<x.getCharacter()->getBlood()<<std::endl;
    x.getCharacter()->setBlood(1000);
    std::cout << "blood" <<x.getCharacter()->getBlood()<<std::endl;

    Character *y = new CharacterB(120);
    x.setCharacter(y);
    x.getCharacter()->Hart();

    std::cout << "blood" <<x.getCharacter()->getBlood()<<std::endl;

    return 0;
}
